package com.hbzhit.config;

import com.hbzhit.common.shiro.ShiroSessionDAO;
import com.hbzhit.common.shiro.ShiroSessionManager;
import com.hbzhit.common.shiro.filter.LoginFilter;
import com.hbzhit.common.utils.ConfigConstant;
import com.hbzhit.modules.sys.oauth2.OAuth2Realm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Shiro配置
 *
 * @author Tiger Zhang
 */
@Configuration
public class ShiroConfig {

    @Bean
    public SecurityManager securityManager(OAuth2Realm oAuth2Realm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(oAuth2Realm);
        securityManager.setRememberMeManager(null);
        securityManager.setSessionManager(sessionManager());
        return securityManager;
    }

    /**
     * 注入session管理器
     */
    @Bean
    public SessionManager sessionManager() {
        DefaultSessionManager sessionManager = new ShiroSessionManager();
        //定时清除无效的session
        sessionManager.setSessionValidationSchedulerEnabled(true);
        //删除无效的session
        sessionManager.setDeleteInvalidSessions(true);
        //会话数据处理类
        sessionManager.setSessionDAO(sessionDAO());
        //全局会话超时时间（单位毫秒）
        sessionManager.setGlobalSessionTimeout(1000 * 60 * 60 * 24);
        return sessionManager;
    }

    /**
     * 注入session数据库存储dao
     */
    @Bean
    public SessionDAO sessionDAO(){
        return new ShiroSessionDAO();
    }

    /**
     * 注入自定义的Realm类
     **/
    @Bean
    public OAuth2Realm oAuth2Realm() {
        return new OAuth2Realm();
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        //oauth过滤
        Map<String, Filter> filters = new HashMap<>();
        filters.put("authc", new LoginFilter());
//        filters.put("oauth2", new OAuth2Filter());
        shiroFilter.setFilters(filters);

        Map<String, String> filterMap = new LinkedHashMap<>();

        filterMap.put("/login/**", "anon"); // 登陆相关
        filterMap.put("/comm/**", "anon"); // 公共接口
        filterMap.put("/sys/logo/info", "anon"); // logo
        filterMap.put(ConfigConstant.LOCAL_STORAGE_ROOT + "/**", "anon"); // 本地文件

        filterMap.put("/sys/wechat/**", "anon");
        filterMap.put("/wx/**", "anon");

/*
        filterMap.put("/ueditor/**", "anon");

        filterMap.put("/druid/**", "anon");

        filterMap.put("/swagger/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/swagger-resources/**", "anon");

        filterMap.put("/activiti/**", "anon");
        filterMap.put("/act/**", "anon");

        filterMap.put("/magic/**", "anon");
*/

        filterMap.put("/**", "authc");
        // filterMap.put("/**", "anon");
        shiroFilter.setFilterChainDefinitionMap(filterMap);

        return shiroFilter;
    }

    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
